ifeq ($(yt_srctree),)
# 上面两层
yt_srctree := $(patsubst %/,%,$(dir $(CURDIR)))
yt_srctree := $(patsubst %/,%,$(dir $(yt_srctree)))
yt_srctree := $(patsubst %/,%,$(dir $(yt_srctree)))
endif

BPF_BUILF_CONFIG_FILE ?= $(yt_srctree)/hacks/yt_build_bpf_yocto_v1.mk
include ${BPF_BUILF_CONFIG_FILE}


EXECABLE ?= monitor-exec

BPFCODE = helloworld.bpf





# BPFSO = ${liba_path} /home/edgarzhou/work/gitee/bootstrap/bpftool/src/libbpf/libbpf.a 

# Setting -DHAVE_ATTR_TEST=0 for the kernel containing below patch:
# 06f84d1989b7 perf tools: Make usage of test_attr__* optional for perf-sys.h
#
# The patch was included in Linus's tree starting v5.5-rc1, but was also included
# in stable kernel branch linux-5.4.y. So it's hard to determine whether a kernel
# is affected based on the kernel version alone:
# - for a v5.4 kernel from Linus's tree, no;
# - for a v5.4 kernel from the stable tree (used by many distros), yes.
#
# So let's look at the actual kernel source code to decide.
#
# See more context at:
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=06f84d1989b7e58d56fa2e448664585749d41221
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fce9501aec6bdda45ef3a5e365a5e0de7de7fe2d
CFLAGS += $(shell grep -q "define HAVE_ATTR_TEST 1" ${KERNEL_SRC_PATH}/tools/perf/perf-sys.h \
                  && echo "-DHAVE_ATTR_TEST=0")

.PHONY: clean $(CLANG) bpfload build

clean:
	rm -f *.o *.so $(EXECABLE)

build: clean bpfload
	$(CLANG) -O2 -target bpf -c $(BPFCODE:=.c) $(CCINCLUDE) -o ${BPFCODE:=.o}

bpfload: 
	${CC} $(CFLAGS) -o $(EXECABLE) $(LOADINCLUDE) $(BPFLOADER) loader.c ${liba_path}

push:
	adb push ${EXECABLE} ${BPFCODE:=.o} /data/
	adb shell sync
	adb shell "cd /data && scp ${EXECABLE} ${BPFCODE:=.o} root@192.168.100.1:~/"

$(EXECABLE): bpfload

.DEFAULT_GOAL := build
